<?php

namespace app\admin\controller\base;

use app\admin\model\Student;
use app\common\controller\Backend;

/**
 * 专业
 *
 * Class Major
 * @package app\admin\controller\base
 */
class Major extends Backend
{
    /**
     * @var \app\common\model\Major
     */
    protected $model = null;
    protected $searchFields = 'name';
    protected $noNeedRight = ['searchList', 'cxSelect'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Major');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {


            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model->where($where)->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {

                $params['creator_id'] = $this->auth->id;

                $result = $this->model->validate('Major.add')->save($params);

                if ($result === false) {
                    $this->error($this->model->getError());
                }
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                $result = $row->validate('Major.edit')->save($params);
                if ($result === false) {
                    $this->error($row->getError());
                }
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $ids = explode(',', $ids);
            $ids = array_unique($ids);

            $student_count = Student::whereIn('major_id', $ids)->count();
            if ($student_count) $this->error('此专业下有学生数据,不能删除');

            $count = $this->model->where('id', 'in', $ids)->delete();
            if ($count) {
                $this->success();
            }
        }
        $this->error();
    }

    /**
     * 搜索下拉列表
     */
    public function searchList()
    {
        $result = $this->getData();
        $searchList = [];
        foreach ($result as $key => $value) {
            $searchList[] = ['id' => $value['number'], 'name' => $value['name']];
        }
        $data = ['searchlist' => $searchList];
        $this->success('', null, $data);
    }

    /**
     * 联动搜索
     */
    public function cxSelect()
    {
        $result = $this->getData();
        $searchList = [];
        foreach ($result as $key => $value) {
            $searchList[] = ['value' => $value['number'], 'name' => $value['name']];
        }

        $this->success('', null, $searchList);
    }


    public function getData()
    {
        $result = $this->model->all();

        return $result;
    }




}
